﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

namespace YJKDictModule.Operation
{
	public class DictOperation
	{
		public DictOperation(string strFileName)
		{
			this.fileName = strFileName;
		}

		public static List<string> GetAllEntry(XmlNode xn)
		{
			List<string> list = new List<string>();
			foreach (object obj in xn.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Name == "词条")
				{
					list.Add(xmlNode.Attributes["name"].Value);
				}
			}
			return list;
		}

		public static List<string> GetAllDirectory(XmlNode xn)
		{
			List<string> list = new List<string>();
			foreach (object obj in xn.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Name == "目录")
				{
					list.Add(xmlNode.Attributes["name"].Value);
				}
			}
			return list;
		}

		public void FillTree(TreeView tree)
		{
			tree.Nodes.Clear();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.fileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("盈建科词库");
			TreeNode treeNode = new TreeNode(xmlNode.Attributes["name"].Value);
			treeNode.Text = xmlNode.Attributes["name"].Value;
			treeNode.Tag = xmlNode;
			this.SetChildNode(treeNode, xmlNode);
			tree.Nodes.Add(treeNode);
			tree.ExpandAll();
			tree.SelectedNode = tree.Nodes[0];
		}

		public void FillCbx(ComboBox cbx)
		{
			List<string> list = new List<string>();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.fileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("盈建科词库");
			foreach (object obj in xmlNode)
			{
				XmlNode xmlNode2 = (XmlNode)obj;
				if (xmlNode2.Attributes["name"].Value == "最近使用的词")
				{
					foreach (object obj2 in xmlNode2.ChildNodes)
					{
						XmlNode xmlNode3 = (XmlNode)obj2;
						if (xmlNode3.Name == "词条")
						{
							list.Add(xmlNode3.Attributes["name"].Value);
						}
					}
				}
			}
			cbx.Items.Clear();
			foreach (string item in list)
			{
				cbx.Items.Insert(0, item);
			}
		}

		public static bool AddStringEntry(XmlNode xn, StringEntry se, string dictFileName)
		{
			bool result;
			try
			{
				if (DictOperation.GetAllEntry(xn).Contains(se.Name))
				{
					result = false;
				}
				else
				{
					XmlElement xmlElement = xn.OwnerDocument.CreateElement("词条");
					xmlElement.SetAttribute("name", se.Name);
					xmlElement.InnerText = se.Text;
					xn.AppendChild(xmlElement);
					xn.OwnerDocument.Save(dictFileName);
					result = true;
				}
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public static bool AddSubDirectory(XmlNode xn, string name, string dictFileName)
		{
			bool result;
			try
			{
				if (DictOperation.GetAllDirectory(xn).Contains(name))
				{
					result = false;
				}
				else
				{
					XmlElement xmlElement = xn.OwnerDocument.CreateElement("目录");
					xmlElement.SetAttribute("name", name);
					xn.AppendChild(xmlElement);
					xn.OwnerDocument.Save(dictFileName);
					result = true;
				}
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public static bool RemoveDirectory(XmlNode parentNode, XmlNode childNode, string dictFileName)
		{
			bool result;
			try
			{
				parentNode.RemoveChild(childNode);
				parentNode.OwnerDocument.Save(dictFileName);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public static bool RemoveStringEntry(XmlNode parentNode, XmlNode childNode, string dictFileName)
		{
			bool result;
			try
			{
				parentNode.RemoveChild(childNode);
				parentNode.OwnerDocument.Save(dictFileName);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public static bool RemoveFirstStringEntry(XmlNode parentNode, string dictFileName)
		{
			bool result;
			try
			{
				parentNode.RemoveChild(parentNode.FirstChild);
				parentNode.OwnerDocument.Save(dictFileName);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public static bool EditDirectory(XmlNode node, string name, string dictFileName)
		{
			bool result;
			try
			{
				if (DictOperation.GetAllDirectory(node).Contains(name))
				{
					result = false;
				}
				else
				{
					node.Attributes["name"].Value = name;
					node.OwnerDocument.Save(dictFileName);
					result = true;
				}
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public static bool EditStringEntry(XmlNode node, string name, string interText, string dictFileName, bool duplicateName)
		{
			bool result;
			try
			{
				if (duplicateName && DictOperation.GetAllEntry(node.ParentNode).Contains(name))
				{
					result = false;
				}
				else
				{
					node.Attributes["name"].Value = name;
					node.InnerText = interText;
					node.OwnerDocument.Save(dictFileName);
					result = true;
				}
			}
			catch
			{
				result = false;
			}
			return result;
		}

		private void SetChildNode(TreeNode tn, XmlNode xn)
		{
			foreach (object obj in xn.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Name == "目录")
				{
					TreeNode treeNode = new TreeNode(xmlNode.Attributes["name"].Value);
					treeNode.Text = xmlNode.Attributes["name"].Value;
					treeNode.Tag = xmlNode;
					this.SetChildNode(treeNode, xmlNode);
					tn.Nodes.Add(treeNode);
				}
			}
		}

		private const string RootName = "盈建科词库";

		private string fileName;
	}
}
